#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
map<LL,LL>map1;
LL a[maxn],b[maxn];
bool vis[maxn];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL t;cin>>t;
   while(t--){
      map1.clear();
      LL n,m;cin>>n>>m;
      for(LL i=0;i<=n+10;i++) vis[i]=0;
      for(LL i=1;i<=n;i++) cin>>a[i],map1[a[i]]=i;
      for(LL i=1;i<=m;i++) cin>>b[i];
      LL ans=0;LL res=0;
      for(LL i=1;i<=m;i++){
          if(!vis[i]){
             vis[i]=true;
             if(i==1) ans+=(map1[b[i]]-1)*2+1,res=map1[b[i]]-1;
             else ans+=res*2+1;
             LL temp=0;
             for(LL j=i+1;j<=m;j++){
                 if(map1[b[j]]<map1[b[i]]){
                    ans++;
                    temp++;
                    vis[j]=true;
                 }
                 else{
                    res=(res-temp+map1[b[j]]-map1[b[i]]-1);
                    break;
                 }
             }
          }
      }
      cout<<ans<<"\n";
   }
   return 0;
}
